﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Data;
using System.Globalization;
using System.Collections;

namespace Asistente.Converters
{
    public class PanelMasterDetailsCollectionConverter : IMultiValueConverter
    {   
        /// <summary>
        /// This converter merge collections of type panel and MasterDetails
        /// </summary>
        /// <returns></returns>
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            List<object> lista = values.ToList<Object>();
            var col = new List<Object>();

            foreach (object item in lista)
            {
                if ((item != null))
                {
                    if (typeof(IEnumerable).IsAssignableFrom(item.GetType()))
                    {
                        var enumerator = ((IEnumerable)item).GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            col.Add(enumerator.Current);
                        }
                    }
                    else
                    {
                        col.Add(item);
                    }
                }
            }
            return col;
            //throw new NotImplementedException();
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
